package com.itheima.controller;

import com.itheima.common.R;
import com.itheima.dto.InsertSetmealDTO;
import com.itheima.dto.UpdateSetmealDTO;
import com.itheima.service.SetmealService;
import com.itheima.vo.SetmealVO;
import com.itheima.vo.SetmealVoPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author msi
 */
@RequestMapping("/setmeal")
@RestController
public class SetmealController {
    @Autowired
    private SetmealService service;

    @PostMapping("")
    public R<String> insertSetmeal(@RequestBody InsertSetmealDTO insertSetmealDTO) {

        boolean b = service.insertSetmeal(insertSetmealDTO);

        return b ? R.success("新增套餐成功") : R.error("操作失败");
    }

    @GetMapping("/page")
    public R<SetmealVoPage> getPageList(@RequestParam(value = "name", required = false) String name, @RequestParam("page") Integer page, @RequestParam("pageSize") Integer pageSize) {
        SetmealVoPage pageList = service.getPageList(name, page, pageSize);

        return R.success(pageList);
    }

    @PutMapping("")
    public R<String> updateSetmeal(@RequestBody UpdateSetmealDTO updateSetmealDTO) {

        Boolean flag = service.updateSetmeal(updateSetmealDTO);

        return flag ? R.success("修改成功") : R.error("操作失败");
    }

    @GetMapping("/{id}")
    public R<SetmealVO> getSetmeal(@PathVariable String id) {
        SetmealVO setmeal = service.getSetmeal(id);

        return R.success(setmeal);
    }

    @PostMapping("/status/{status}")
    public R<String> changeStatus(@PathVariable Integer status, @RequestParam("ids") String ids) {
        Boolean flag = service.changeStatus(status, ids);

        return flag ? R.success("操作成功") : R.error("操作失败");
    }

    @DeleteMapping("")
    public R<String> delete(@RequestParam("ids") String[] ids) {
        Boolean flag = service.delete(ids);

        return flag ? R.success("操作成功") : R.error("操作失败");
    }
}
